using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESBSimpleSamples.ServiceComponents.Encoding;

using System.Xml;

namespace ESBSimpleSamples.Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length != 1)
            {
                MessageBox.Show("Input string must be a single character");
            }
            else
            {
                StringBuilder outputBuilder = new StringBuilder();
                DateTime startTime = DateTime.Now;
                foreach (KeyValuePair<string, byte[]> serviceResponse in this.GetServiceResponses(this.textBox1.Text))
                {
                    outputBuilder.AppendFormat("Service: {0}, Codes:", serviceResponse.Key);
                    foreach (byte code in serviceResponse.Value)
                    {
                        outputBuilder.AppendFormat(" {0}", code);
                    }
                    outputBuilder.AppendLine();
                }
                DateTime endTime = DateTime.Now;
                outputBuilder.AppendFormat("Responses took: {0} seconds", new TimeSpan(endTime.Ticks - startTime.Ticks).TotalSeconds);
                outputBuilder.AppendFormat("{0}*{0}", Environment.NewLine);
                this.textBox2.Text += outputBuilder.ToString();
            }
        }

        private Dictionary<string, byte[]> GetServiceResponses(string character)
        {
            Dictionary<string, byte[]> serviceResponses = new Dictionary<string, byte[]>();
            //in the real word, use factory or Windsor for this:
            switch (this.comboBox1.SelectedItem.ToString())
            {
                case "InProcess":
                    serviceResponses.Add("ASCIIService", new ASCIIService().GetCharacterCode(character));
                    serviceResponses.Add("UnicodeService", new UnicodeService().GetCharacterCode(character));
                    break;

                case "WebService":
                    EncodingWS.Service1 webService = new EncodingWS.Service1();
                    webService.Url = "http://localhost/ESBSimpleSamples/EncodingService.asmx";
                    serviceResponses.Add("EncodingWS.GetASCIICode", webService.GetASCIICode(character));
                    serviceResponses.Add("EncodingWS.GetUnicodeCode", webService.GetUnicodeCode(character));
                    break;

                case "WCF":
                    EncodingWCF.EncodingServiceClient wcfService = new ESBSimpleSamples.Client.EncodingWCF.EncodingServiceClient();
                    serviceResponses.Add("WcfASCIIService", wcfService.GetASCIICode(character));
                    serviceResponses.Add("WcfUnicodeService", wcfService.GetUnicodeCode(character));
                    break;

                case "ESB":
                    ServiceRequests.GetASCIICode esbASCII = new ServiceRequests.GetASCIICode();
                    esbASCII.Request.Character = character;
                    serviceResponses.Add("EsbASCIIService", esbASCII.Response.GetASCIICodeResult);

                    ServiceRequests.GetUnicodeCode esbUnicode = new ServiceRequests.GetUnicodeCode();
                    esbUnicode.Request.Character = character;
                    serviceResponses.Add("EsbUnicodeService", esbUnicode.Response.GetUnicodeCodeResult);
                    break;

                case "ESB.WCF":
                    ServiceRequests.Wcf.GetASCIICode esbASCIIWcf = new ServiceRequests.Wcf.GetASCIICode();
                    esbASCIIWcf.Request.Character = character;
                    serviceResponses.Add("EsbWcfASCIIService", esbASCIIWcf.Response.GetASCIICodeResult);

                    ServiceRequests.Wcf.GetUnicodeCode esbUnicodeWcf = new ServiceRequests.Wcf.GetUnicodeCode();
                    esbUnicodeWcf.Request.Character = character;
                    serviceResponses.Add("EsbWcfUnicodeService", esbUnicodeWcf.Response.GetUnicodeCodeResult);
                    break;
            }
            return serviceResponses;
        }
    }
}